package com.chang.bootplus.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chang.bootplus.base.utils.ApiResponse;
import com.chang.bootplus.base.utils.EvaulateMap;
import com.chang.bootplus.entity.UserResultmap;
import com.chang.bootplus.mapper.BmiMapper;
import com.chang.bootplus.mapper.UserResultmapMapper;
import com.chang.bootplus.po.CalculateAllPO;
import com.chang.bootplus.po.CalculatePO;
import com.chang.bootplus.service.UserResultmapService;
import com.chang.bootplus.vo.CalculateAllVO;
import com.chang.bootplus.vo.CalculateVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chang
 * @since 2022-02-05
 */
@Service
public class UserResultmapServiceImp extends ServiceImpl<UserResultmapMapper, UserResultmap> implements UserResultmapService {


    @Resource
    UserResultmapMapper userResultmapMapper;

    @Resource
    BmiMapper bmiMapper;

    @Override
    public ApiResponse<CalculateVO> calculate(CalculatePO po) {
        UserResultmap userResultmap = null;
        EvaulateMap evaulateMap = new EvaulateMap();
        CalculateVO calculateVO=new CalculateVO();
        // 7为bmi
        if (po.getProjectId()==7){
            // 向上取整并保留一位小数,防止22.96找不到   --->

            BigDecimal bd1 = new BigDecimal(Double.toString(po.getResult()));
            double v = bd1.multiply(new BigDecimal(10.0)).doubleValue();// 22.96 --> 229.6
            double ceil = Math.ceil(v); // 229.6---> 230
            Double newResult= new BigDecimal(ceil).divide(new BigDecimal(10.0)).doubleValue(); //230 --> 23
            Integer level = bmiMapper.selectLevelByValue(po.getSex(), po.getClassId(), newResult);
            if (level==1){
                calculateVO.setScore(100).setLevel("正常");
            }else if (level==2){
                calculateVO.setScore(80).setLevel("低体重");
            }else if (level==3){
                calculateVO.setScore(80).setLevel("超重");
            }else if (level==4){
                calculateVO.setScore(60).setLevel("肥胖");
            }
        }else{
            //
            if (po.getProjectId()==2||po.getProjectId()==6){
                userResultmap=userResultmapMapper.selectAllByIdsRun(po.getClassId(), po.getSex(), po.getProjectId(), po.getResult());
                if (userResultmap==null){
                    userResultmap=userResultmapMapper.selectAllByIdsRun2(po.getClassId(), po.getSex(), po.getProjectId(), po.getResult());
                }
                calculateVO=evaulateMap.calculate2(po,userResultmap);
            }else {
                // 除了2，6，7
                userResultmap=userResultmapMapper.selectAllByIds(po.getClassId(), po.getSex(), po.getProjectId(), po.getResult());
                if (userResultmap==null){
                    userResultmap=userResultmapMapper.selectAllByIds2(po.getClassId(), po.getSex(), po.getProjectId(), po.getResult());
                }
                calculateVO= evaulateMap.calculate(po,userResultmap);
            }
        }
        return new ApiResponse<>(calculateVO);
    }


    @Override
    public ApiResponse<CalculateAllVO> calculateAll(CalculateAllPO po) {
        EvaulateMap evaulateMap = new EvaulateMap();
        Double allScore = evaulateMap.getAllScore2(po.getFifty(), po.getBmi(), po.getVital(), po.getSitUp(), po.getJump(), po.getRunback(), po.getSitRech(), po.getGradeNum());
        CalculateAllVO calculateAllVO = new CalculateAllVO();
        calculateAllVO.setScoreAll(allScore);
        calculateAllVO.setLevel(EvaulateMap.getLevelStringByLevelId(EvaulateMap.getLevel(allScore.intValue())));
        calculateAllVO.setLevenum(EvaulateMap.getLevel(allScore.intValue()));
        return new ApiResponse<>(calculateAllVO);
    }
}
